﻿using Microsoft.AspNetCore.Mvc;
using System;
using Think9.Models;
using Think9.Services.Base;
using Think9.Services.Basic;
using Think9.Services.Table;

namespace Think9.Controllers.Basic
{
    [Area("SysTable")]
    public class RuleMultipleController : BaseController
    {
        private RuleMultipleService ruleMultipleService = new RuleMultipleService();
        private RuleServiceBasic ruleService = new RuleServiceBasic();
        private RuleListService ruleListService = new RuleListService();
        private ComService comService = new ComService();

        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.Guid = Think9.Services.Basic.CreatCode.NewGuid();
            ViewBag.SelectSourceList = ruleService.GetSelectSource();

            return View();
        }

        [HttpPost]
        public ActionResult Add(RuleMultipleEntity model, string id)
        {
            string err = "";
            model.ValueFiled = BasicHelp.GetSplitStrByChar(model.ValueFiled, ".", 1);//获得分割后字符
            model.TxtFiled = BasicHelp.GetSplitStrByChar(model.TxtFiled, ".", 1);//获得分割后字符

            if (comService.GetTotal("sys_temp", "where Guid='" + id + "'") == 0)
            {
                return Json(ErrorTip("未添加列表字段"));
            }

            try
            {
                err = ruleMultipleService.Add(model, id);
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }

            if (err == "")
            {
                return Json(SuccessTip("新建成功"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        [HttpGet]
        public ActionResult Edit(string rid)
        {
            string where = "where RuleId=@RuleId";
            object param = new { RuleId = rid };

            var model = ruleMultipleService.GetByWhereFirst(where, param);
            var modelList = ruleListService.GetByWhereFirst(where, param);

            if (model != null && modelList != null)
            {
                ViewBag.id = rid;
                ViewBag.SelectSourceList = ruleService.GetSelectValueByTbID(model.TbId);
                ruleMultipleService.SetDefault(ref model);
                model.Name = modelList.RuleName;
                model.showDetails = model.showDetails == null ? "2" : model.showDetails;
                return View(model);
            }
            else
            {
                return Json(ErrorTip("数据不存在！！！"));
            }
        }

        [HttpPost]
        public ActionResult Edit(RuleMultipleEntity model, string id)
        {
            string err = "";

            if (comService.GetTotal("rulemultiplefiled", "where RuleId='" + model.RuleId + "'") == 0)
            {
                return Json(ErrorTip("未添加列表字段"));
            }

            try
            {
                err = ruleMultipleService.Edit(model);
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }

            if (err == "")
            {
                return Json(SuccessTip("编辑成功"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        [HttpPost]
        public ActionResult AddListFiledTemp(string id, string value, string text)
        {
            string err = "";
            text = BasicHelp.GetSplitStrByChar(text, " - ", 1);//获得分割后字符
            if (text.Length > 10)
            {
                text = text.Substring(0, 10);
            }

            try
            {
                err = ruleMultipleService.AddListFiledTemp(id, value, text);
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }

            if (err == "")
            {
                return Json(SuccessTip("操作成功"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        [HttpPost]
        public ActionResult AddListFiled(string id, string value, string text, string tbid)
        {
            string err = "";
            text = BasicHelp.GetSplitStrByChar(text, " - ", 1);//获得分割后字符
            if (text.Length > 15)
            {
                text = text.Substring(0, 15);
            }

            try
            {
                err = ruleMultipleService.AddListFiled(id, value, text, tbid);
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }

            if (err == "")
            {
                return Json(SuccessTip("操作成功"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        [HttpGet]
        public ActionResult DeleteFiledTemp(string id)
        {
            string err = ruleMultipleService.DelListFiledTemp(id);

            if (err == "")
            {
                return Json(SuccessTip(""));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        [HttpGet]
        public ActionResult DeleteFiled(string rid, string indexid)
        {
            string err = ruleMultipleService.DelListFiled(rid, indexid);

            if (err == "")
            {
                return Json(SuccessTip(""));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        [HttpGet]
        public ActionResult GetListFiledTemp(string id)
        {
            var result = new { code = 0, msg = "", count = 999999, data = ruleMultipleService.GetListFiledTemp(id) };
            return Json(result);
        }

        [HttpGet]
        public ActionResult GetListFiled(string id)
        {
            var result = new { code = 0, msg = "", count = 999999, data = ruleMultipleService.GetListFiled(id) };
            return Json(result);
        }

        [HttpGet]
        public JsonResult GetSelectValueFieldByTb(string id)
        {
            return Json(ruleService.GetSelectValueFieldByTb(id == null ? "" : id));
        }

        [HttpGet]
        public JsonResult GetSelectTxtFieldByTb(string id)
        {
            return Json(ruleService.GetSelectTxtFieldByTb(id == null ? "" : id));
        }

        [HttpGet]
        public JsonResult GetSelectOrderFieldByTb(string id)
        {
            return Json(ruleService.GetSelectOrderFieldByTb(id == null ? "" : id));
        }

        [HttpGet]
        public JsonResult GetConditionFieldList(string id)
        {
            string prefix = "";
            return Json(ruleService.GetConditionFieldList(id == null ? "" : id, prefix));
        }
    }
}